﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mds.ServiceInterface;
using mds.BaseFrameModel;

namespace mds.LoginSecurityService
{
    public class LoginProvider : ILoginProvider
    {
         public LoginProvider()
        {
           
        }
       

        public OperationResult<Guid> Login(string userName, string passWord, string otherCode, string inputOtherCode, EnumLoginType loginType)
        {
            OperationResult<Guid> result = new OperationResult<Guid>(Guid.Empty,false);
            int userId= UserInfoDal.CheckUser(userName, passWord, otherCode, inputOtherCode);
            if (userId>0)
            {
                result.ActionResult = true;
                result.Data = Guid.NewGuid();
            } else {
                result.Message = "用户校验失败";
                return result;
            }
            if (result.ActionResult)
            {
                if (loginType == EnumLoginType.Multiple) {
                    if (!LoginCacheProvider.Instance.SaveSession(result.Data, userId).ActionResult) {
                        result.ActionResult = false;
                        result.Data = Guid.Empty;
                        result.Message = "会话缓存失败";
                    }
                }
                if (loginType == EnumLoginType.One) {
                    LoginCacheProvider.Instance.ClearSessions(userId);
                    if (!LoginCacheProvider.Instance.SaveSession(result.Data, userId).ActionResult)
                    {
                        result.ActionResult = false;
                        result.Data = Guid.Empty;
                        result.Message = "会话缓存失败";
                    }
                }
            }
            return result;
        }

        public OperationResult<bool> Logout(Guid sessionId)
        {
           return LoginCacheProvider.Instance.ClearSession(sessionId);
        }

        public OperationResult<Guid> ReSetSession(Guid sessionId)
        {
          var userId=LoginCacheProvider.Instance.GetUserId(sessionId);
          LoginCacheProvider.Instance.ClearSession(sessionId);
          OperationResult<Guid> result = new OperationResult<Guid>(Guid.Empty, false);
          result.ActionResult = true;
          result.Data = Guid.NewGuid();
          if (!LoginCacheProvider.Instance.SaveSession(result.Data, userId).ActionResult)
          {
              result.ActionResult = false;
              result.Data = Guid.Empty;
              result.Message = "会话缓存失败";
          }
          return result;
        }

        public OperationResult<Boolean> IsLogin(Guid sessionId)
        {
            var userId = LoginCacheProvider.Instance.GetUserId(sessionId);
            OperationResult<Boolean> result = new OperationResult<Boolean>(false);
            result.Message = "会话不存在";
            if (userId > 0)
            {
                result.ActionResult = true;
            }
            return result;
        }
    }
}
